//
//  LFBTabBar.m
//  LFBLoveFreshBee
//
//  Created by 丁鼎 on 16/8/18.
//  Copyright © 2016年 丁鼎. All rights reserved.
//

#import "LFBTabBar.h"
#import "LFBShopppingTrolleyBtn.h"

@interface LFBTabBar ()


@property (nonatomic,strong)LFBShopppingTrolleyBtn *Btn;

@end

@implementation LFBTabBar

-(LFBShopppingTrolleyBtn *)Btn{
    
    if(!_Btn)
    {
        _Btn = [[LFBShopppingTrolleyBtn alloc]init];
        
        _Btn.titleLabel.font = [UIFont systemFontOfSize:10];
        
        [_Btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_Btn setTitle:@"购物车" forState:UIControlStateNormal];
        
        [_Btn setImage:[UIImage imageNamed:@"shopCart"] forState:UIControlStateNormal];
        
        [_Btn addTarget:self action:@selector(presentShopCarVC) forControlEvents:UIControlEventTouchUpInside];
    }
    return _Btn;
  
}

-(instancetype)init{
    if(self = [super init])
    {
        [self addSubview:self.Btn];
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
   
    CGFloat height = 48;
    CGFloat width = KScreenWidth/4;
    
    CGRect rect =  CGRectMake(0, 0, width, height);
    
    CGFloat index =  0;
    
    for (UIControl *tabBarButton in self.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            tabBarButton.frame = CGRectOffset(rect, index * width , 0);
            
            index += (index == 1 ? 2: 1);
            
            [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    self.Btn.frame = CGRectOffset(rect, 2 * width, 5);
}

- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
    
    
    for (UIView *imageView in tabBarButton.subviews) {
        
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            
            
            
            //需要实现的帧动画,这里根据需求自定义
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
            animation.duration = 1;
            animation.calculationMode = kCAAnimationCubic;
            //把动画添加上去就OK了
            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
}

-(void)presentShopCarVC{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"presentedVC" object:nil];

}


@end
